package Thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//模拟实现线程常量池
class MyThreadPool{
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                try {
                    while (true){
                        Runnable task=queue.take();
                        task.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.setDaemon(true);
            t.start();
        }
    }
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
 }
public class Demo13 {
    public static void main(String[] args) throws InterruptedException {
    MyThreadPool myThreadPool=new MyThreadPool(5);
        for (int i = 0; i < 1000; i++) {
            final int id=i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    String name=Thread.currentThread().getName();
                    System.out.println("hello"+name+","+id);
                }
            });
        }
        Thread.sleep(1000);
    }
}
